package sweetvsti;

import java.io.File;
import java.util.Map;
import java.util.TreeMap;
import jvst.wrapper.VSTPluginAdapter;

/**
 *
 * @author uimac
 */
public class Settings {

    private static Settings instance = null;

    public static Settings instance() {
        if (instance == null) {
            instance = new Settings();
        }
        return instance;
    }
    
    private int width = 800;
    private int height = 600;
    
    // Waveファイルを入れているディレクトリ
    private String waveBaseDir = "E:\\tmp\\111";
    // Waveファイルを出力するディレクトリ
    private String waveOutDir = "F:\\work\\waveoutdir";
    // 入力waveファイル
    private String waveInFile = "";
    // SweetVSTiのインスタンスにはアクセスできないようなので、ここに変更したかどうかのフラグを立てている
    private boolean isWaveBaseDirChanged = false;
    
    private VSTPluginAdapter pluginAdapter = null;
    // 10オクターブ x 12鍵盤
    private final int KEYBOARDS = 120;
    // 発音数
    private final int CHARACTERS = 143;
    // 読み込むファイル
    private File[][] files = new File[KEYBOARDS][CHARACTERS];
    private int charactor = 0;
    private Map kanaMap = new TreeMap();
    private Map qwertyKanaMap = new TreeMap();
    private String kana[] = {
        "あ",
        "い",
        "う",
        "え",
        "お",
        "か",
        "き",
        "く",
        "け",
        "こ",
        "さ",
        "し",
        "す",
        "せ",
        "そ",
        "た",
        "ち",
        "つ",
        "て",
        "と",
        "な",
        "に",
        "ぬ",
        "ね",
        "の",
        "は",
        "ひ",
        "ふ",
        "へ",
        "ほ",
        "ま",
        "み",
        "む",
        "め",
        "も",
        "や",
        "",
        "ゆ",
        "いぇ",
        "よ",
        "ら",
        "り",
        "る",
        "れ",
        "ろ",
        "わ",
        "うぃ",
        "",
        "うぇ",
        "を",
        "ぁ",
        "ぃ",
        "ぅ",
        "ぇ",
        "ぉ",
        "きゃ",
        "",
        "きゅ",
        "",
        "きょ",
        "しゃ",
        "すぃ",
        "しゅ",
        "しぇ",
        "しょ",
        "ちゃ",
        "つぃ",
        "ちゅ",
        "ちぇ",
        "ちょ",
        "にゃ",
        "",
        "にゅ",
        "にぇ",
        "にょ",
        "ひゃ",
        "",
        "ひゅ",
        "ひぇ",
        "ひょ",
        "ふぁ",
        "ふぃ",
        "ふゅ",
        "ふぇ",
        "ふぉ",
        "みゃ",
        "",
        "みゅ",
        "みぇ",
        "みょ",
        "りゃ",
        "",
        "りゅ",
        "",
        "りょ",
        "が",
        "ぎ",
        "ぐ",
        "げ",
        "ご",
        "か゜",
        "き゜",
        "ぐ゜",
        "げ゜",
        "こ゜",
        "ざ",
        "じ",
        "ず",
        "ぜ",
        "ぞ",
        "だ",
        "ぢ",
        "づ",
        "で",
        "ど",
        "ば",
        "び",
        "ぶ",
        "べ",
        "ぼ",
        "ん",
        "ん",
        "ん",
        "ん",
        "",
        "ぱ",
        "ぴ",
        "ぷ",
        "ぺ",
        "ぽ",
        "ぎゃ",
        "",
        "ぎゅ",
        "",
        "ぎょ",
        "き゜ゃ",
        "",
        "き゜ゅ",
        "き゜ぇ",
        "き゜ょ",
        "じゃ",
        "ずぃ",
        "じゅ",
        "じぇ",
        "じょ",
        "",
        "でぃ",
        "でゅ",
        "",
        "",
        "びゃ",
        "",
        "びゅ",
        "びぇ",
        "びょ",
        "",
        "ぴゃ",
        "ぴゅ",
        "ぴぇ",
        "ぴょ",
    };
    // qwerty配列のキーコードに対応するカナのリスト
    // インデックス０をキーコード３３とする。
    int qwertyKana[][] = {
        // 44
        {44, 23},//"ね",
        // 45
        {45, 29},//"ほ",
        // 46
        {46, 42},//"る",
        // 47
        {47, 33},//"め",
        // 0(48)～9(57)
        {48, 45},//"わ",
        {49, 22},//"ぬ",
        {50, 27},//"ふ",
        {51, 0},//"あ",
        {52, 2},//"う",
        {53, 3},//"え",
        {54, 4},//"お",
        {55, 35},//"や",
        {56, 37},//"ゆ",
        {57, 39},//"よ",
        // 59
        {59, 43},//"れ",
        // A(65)～Z(90)
        {65, 16},//"ち",
        {66, 9},//"こ",
        {67, 14},//"そ",
        {68, 11},//"し",
        {69, 1},//"い",
        {70, 25},//"は",
        {71, 6},//"き",
        {72, 7},//"く",
        {73, 21},//"に",
        {74, 30},//"ま",
        {75, 24},//"の",
        {76, 41},//"り",
        {77, 34},//"も",
        {78, 31},//"み",
        {79, 40},//"ら",
        {80, 13},//"せ",
        {81, 15},//"た",
        {82, 12},//"す",
        {83, 19},//"と",
        {84, 5},//"か",
        {85, 20},//"な",
        {86, 26},//"ひ",
        {87, 18},//"て",
        {88, 10},//"さ",
        {89, 50},//"ん",
        {90, 17},//"つ",
        // 92
        {92, 44},//"ろ",
        // 93
        {93, 32},//"む",
        // 513
        {513, 8},//"け",
        // 514
        {514, 28},//"へ",
    };

    // コンストラクタ
    private Settings() {
        for (int i = 0; i < kana.length; ++i) {
            this.kanaMap.put(new Integer(i), kana[i]);
            //System.out.println(kanaMap.get(Integer.valueOf(i)));
        }

        for (int i = 0; i < qwertyKana.length; ++i) {
            qwertyKanaMap.put(new Integer(qwertyKana[i][0]), new Integer(qwertyKana[i][1]));
        }

        //System.exit(1);
    }

    /**
     * @return the kanaMap
     */
    public Map getKanaMap() {
        return kanaMap;
    }

    /**
     * @param kanaMap the kanaMap to set
     */
    public void setKanaMap(Map kanaMap) {
        this.kanaMap = kanaMap;
    }

    /**
     * @return the width
     */
    public int getWidth() {
        return width;
    }

    /**
     * @return the height
     */
    public int getHeight() {
        return height;
    }

    /**
     * @return the pluginAdapter
     */
    public VSTPluginAdapter getPluginAdapter() {
        return pluginAdapter;
    }

    /**
     * @param pluginAdapter the pluginAdapter to set
     */
    public void setPluginAdapter(VSTPluginAdapter pluginAdapter) {
        this.pluginAdapter = pluginAdapter;
    }

    /**
     * @return the KEYBOARDS
     */
    public int getKEYBOARDS() {
        return KEYBOARDS;
    }

    /**
     * @return the CHARACTERS
     */
    public int getCHARACTERS() {
        return CHARACTERS;
    }

    /**
     * @return the files
     */
    public File[][] getFiles() {
        return files;
    }

    /**
     * @return the charactor
     */
    public int getCharactor() {
        return charactor;
    }

    /**
     * @param charactor the charactor to set
     */
    public void setCharactor(int charactor) {
        this.charactor = charactor;
    }

    /**
     * @return the qwertyKanaMap
     */
    public Map getQwertyKanaMap() {
        return qwertyKanaMap;
    }

    /**
     * @return the waveBaseDir
     */
    public String getWaveBaseDir() {
        return waveBaseDir;
    }

    /**
     * @param waveBaseDir the waveBaseDir to set
     */
    public void setWaveBaseDir(String waveBaseDir) {
        this.waveBaseDir = waveBaseDir;
    }
    

    /**
     * @return the isWaveBaseDirChanged
     */
    public boolean isIsWaveBaseDirChanged() {
        return isWaveBaseDirChanged;
    }

    /**
     * @param isWaveBaseDirChanged the isWaveBaseDirChanged to set
     */
    public void setIsWaveBaseDirChanged(boolean isWaveBaseDirChanged) {
        this.isWaveBaseDirChanged = isWaveBaseDirChanged;
    }

    /**
     * @return the waveOutDir
     */
    public String getWaveOutDir() {
        return waveOutDir;
    }

    /**
     * @param waveOutDir the waveOutDir to set
     */
    public void setWaveOutDir(String waveOutDir) {
        this.waveOutDir = waveOutDir;
    }

    /**
     * @return the waveInFile
     */
    public String getWaveInFile() {
        return waveInFile;
    }

    /**
     * @param waveInFile the waveInFile to set
     */
    public void setWaveInFile(String waveInFile) {
        this.waveInFile = waveInFile;
    }
    
    public void loadAudioFiles() {
        // 60がC3で、12個で1オクターブ
        int base_keyboard = 0;
        this.files = Settings.instance().getFiles();
        int keyboards = Settings.instance().getKEYBOARDS();
        int charactors = Settings.instance().getCHARACTERS();
        for (int i = 0; i < keyboards; ++i) {
            for (int k = 0; k < charactors; ++k) {
                String file_name = "480_" + (i + base_keyboard) + "_" + (k+1) + ".wav";
                files[i][k] = new File(Settings.instance().getWaveBaseDir() + File.separator + file_name);
            }
        }
    }
}
